package arithmetic.leetCode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-09-09
 * Time: 20:36
 */
public class OddEvenList {

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        node.next = new ListNode(2);
        node.next.next = new ListNode(3);
        node.next.next.next = new ListNode(4);
        node.next.next.next.next = new ListNode(5);
        ListNode result = oddEvenList(node);
        while (result != null) {
            System.out.print(result.val + "\t");
            result = result.next;
        }
    }

    public static ListNode oddEvenList(ListNode head) {
        if (head == null) return null;
        ListNode old = head, event = head.next, eventHead = event;
        while (event != null && event.next != null) {
            old.next = event.next;
            old = old.next;
            event.next = old.next;
            event = event.next;
        }
        old.next = eventHead;
        return head;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
